﻿using System.Configuration;
using System.Net.Configuration;
using System.Web;
using System.Web.Configuration;
using OpenQuarters.WebQuarters.Core;

namespace OpenQuarters.WebQuarters.Providers.Email
{
    public class DotNet1 : IEmailProvider
    {
        /// <summary>
        /// Sends email using depreciated .net 1.1 architecture
        /// </summary>
        /// <param name="to">Semi-colon seperated list of recipient email addresses</param>
        /// <param name="from">Senders email address</param>
        /// <param name="cc">Semi-colon seperated list of carbon copy email addresses</param>
        /// <param name="bcc">Semi-colon seperated list of blind carbon copy email address</param>
        /// <param name="subject">Subject of email</param>
        /// <param name="body">Body content of email</param>
        /// <param name="isHtml">Determine whether email is HTML formatted</param>
        public void SendEmail(string to, string from, string cc, string bcc, string subject, string body, bool isHtml)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

            // Create the mail message.
            System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage();
            mailMessage.From = from;
            mailMessage.To = to;
            mailMessage.Cc = cc;
            mailMessage.Bcc = bcc;
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            if (isHtml)
                mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html;

            // Set authentication.
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //Basic authentication.
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", settings.Smtp.Network.UserName);
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", settings.Smtp.Network.Password);
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", settings.Smtp.Network.Port);

            System.Web.Mail.SmtpMail.SmtpServer = settings.Smtp.Network.Host;

            // Send the email.
            System.Web.Mail.SmtpMail.Send(mailMessage);
        }

    }
}
